package co.yixiang.modules.system.service.impl;

import co.yixiang.modules.system.domain.MainIncome;
import cn.hutool.core.convert.Convert;
import co.yixiang.common.service.impl.BaseServiceImpl;
import lombok.AllArgsConstructor;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.utils.ValidationUtil;
import co.yixiang.utils.FileUtil;
import co.yixiang.modules.system.service.MainIncomeService;
import co.yixiang.modules.system.dto.MainIncomeDto;
import co.yixiang.modules.system.dto.queryCriteria.MainIncomeQueryCriteria;
import co.yixiang.modules.system.mapper.MainIncomeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author db
* @date 2020-10-22
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "mainIncome")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class MainIncomeServiceImpl extends BaseServiceImpl<MainIncomeMapper, MainIncome> implements MainIncomeService {

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(MainIncomeQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<MainIncome> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", Convert.toList(MainIncomeDto.class, page.getList()));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<MainIncome> queryAll(MainIncomeQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(MainIncome.class, criteria));
    }


    @Override
    public void download(List<MainIncomeDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (MainIncomeDto mainIncome : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("关联合伙人 id", mainIncome.getUserId());
            map.put("总收益", mainIncome.getTotalIncome());
            map.put("今日收益", mainIncome.getTodayIncome());
            map.put("昨日收益", mainIncome.getLastIncome());
            map.put("本月收益", mainIncome.getMonthIncome());
            map.put("创建时间", mainIncome.getCreateDate());
            map.put("修改时间", mainIncome.getUpdateDate());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
